<?php
    function buildSyntax($content, $regx)
    {
        preg_match($regx, $content, $match);        
        $smsId = !empty($match[2]) ? intval($match[2]) : 0;                
        
        /* 
        * Kiểm tra sms có tồn tại không 
        * Nếu tồn tại thì lấy ra userId
        */
        
        $user = getUserByKuteId($smsId);        
        
        $result = array();
        $result["userId"] = !empty($user) ? $user["userId"] : 0;
        $result["content"] = !empty($user) ? $user["content"] : "";
        $result["price"] = !empty($user) ? $user["price"] : "15000";
        $result["smsId"] = $smsId;        
        
        return $result;
    }
    
    function getUserByKuteId($id)
    {
        $sql = "SELECT userId, content, price FROM c_sms_kute WHERE id=" . $id;
        $result = mysql_query($sql);
        $rows = mysql_fetch_assoc($result);
        return $rows;
    }
    
    function UpdateDownload($id)
    {
        $sql = "UPDATE c_sms_kute SET download=(download+1) WHERE id=" . $id;
        $result = @mysql_query($sql);            
        return $result;
    }
    
    /*
    * processing
    */
    
    function processing($data)
    {        
        
        $service_codes = array("5000"=>"6522", "10000"=>"6622", "15000"=>"6722");
        $service_codes_8x12 = array("5000"=>"8512", "10000"=>"8612", "15000"=>"8712");
        
        $regx = "/^SMS(\s+)(\d+)$/si";        
        
        if(preg_match($regx, $data["content"])){
            
            $syntax = buildSyntax($data["content"], $regx);
                                                
            $data["userId"] = $syntax["userId"];
            $data["id"] = $syntax["smsId"];            
            
            if(!empty($data["userId"]) && !empty($data["id"])){
                $sc = isset($service_codes[$syntax["price"]]) ? $service_codes[$syntax["price"]] : "6722";
                $sc_8x12 = isset($service_codes_8x12[$syntax["price"]]) ? $service_codes_8x12[$syntax["price"]] : "8712";
                
                $list_service_code = array("6022", "8012", $sc_8x12, $sc);
//                return $sc_8x12; 
                if(in_array($data["serviceCode"], $list_service_code)){
                    
                      
                    $data["status"] = 1; 
                    
                    $response["status"] = 1;                        
                    $response["msg"] = html_entity_decode($syntax["content"], NULL, 'UTF-8');
                    
                    /* Cập nhật lượt download */
                    UpdateDownload($data["id"]);
                      
                } else {
                    $response["status"] = 3;                        
                    $response["msg"] = "Tin nhan khong dung dau so quy dinh";                
                                                            
                    $data["status"] = 0;                 
                }   
            } else {
                $response["status"] = 2;                
                $response["msg"] = "Tin nhan sai cu phap";    
                                                                
                $data["status"] = 0;                
            }   
        } else {
            $response["status"] = 2;                
            $response["msg"] = "Tin nhan sai cu phap";                
                        
            $data["userId"] = 0;                
            $data["status"] = 0;
            $data["id"] = 0;      
        }
        
        /* log sms */
        $data["transactionId"] = $data["transactionId"];
        $data["telco"] = checkTelco($data["sender"]);    
        $data["price"] = priceServiceCode($data["serviceCode"]);    
        $data["partnerId"] = 1; //ID partner HDC            
        $data["type"] = 6;
        
        $data["output"] = $response["status"] . "|" . $data["transactionId"];        
        
        $result = InsertSmsLogTestNoPercent($data);           
        return $response;
    } 
?>